package com.vf.admin.platform.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.platform.entity.RoleMenu;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 角色菜单关联Dao
 */
@Repository
public class RoleMenuDao extends BaseDao<RoleMenu> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 保存或者更新
     */
    public void saveOrUpdate(RoleMenu roleMenu){
        if(roleMenu.getRoleid() > 0){
            this.update(roleMenu);
        }else{
            roleMenu.setRoleid(this.add(roleMenu));
        }
    }
    //详情
    public RoleMenu get(Integer id){
        return this.get(id,RoleMenu.class);
    }

    //列表查询
    public List<RoleMenu> findList(Map<String,Object> paraMap){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_role_menu  where 1=1");
        if(!StringEx.isNullOrLing(paraMap.get("roleid"))){
            sbStr.append(" and roleid = ? ");
            paramList.add(paraMap.get("roleid"));
        }
        if(!StringEx.isNullOrLing(paraMap.get("menuid"))){
            sbStr.append(" and menuid = ? ");
            paramList.add(paraMap.get("menuid"));
        }
        String sql = sbStr.toString();
        return this.queryList(sql,paramList,RoleMenu.class);
    }

    /**
     * 为角色分配的菜单
     *
     * @return
     */
    public void save(List<RoleMenu> roleMenuList) {
        try {
            if(roleMenuList == null || roleMenuList.size()<=0){
                return ;
            }
            ArrayList<Object> paramList = new ArrayList<Object>();
            StringBuilder sbSql = new StringBuilder("insert into bt_role_menu (roleid,menuid)  values");
            for (RoleMenu roleMenu : roleMenuList) {
                if (roleMenu==null) {
                    continue;
                }
                sbSql.append(" (?,?) ,");
                paramList.add(roleMenu.getRoleid());
                paramList.add(roleMenu.getMenuid());
            }
            String sql = sbSql.substring(0,sbSql.lastIndexOf(","));
            this.update(sql, paramList);
        } catch (Exception e) {
            logger.error("为角色分配的菜单权限时异常",e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据roleid删除
     */
    public void delByRoleId(int id){
        if (id <= 0) {
            return;
        }
        String sql = "delete  from bt_role_menu where roleid = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    /**
     * 根据menuid 删除
     */
    public void delByMenuId(int menuid){
        if (menuid <= 0) {
            return;
        }
        String sql = "delete  from bt_role_menu where menuid = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(menuid);
        this.del(sql, paramList);
    }

}
